package com.xlinur.android.moviecatalog;
//TODO отображение других полей пример оригинальное название
import com.xlinur.android.moviecatalog.R;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

public class PreferencesView extends PreferenceActivity {
	private static String DIVIDER = ",";
	private static String strAll;

	private static String getEntityByValue(Preference preference, String value) {
		String result = "";
		ListPreference list = (ListPreference) preference;
		CharSequence[] entities = list.getEntries();
		CharSequence[] values = list.getEntryValues();
		int size = entities.length;
		int i;
		for (i = 0; i < size && !value.contentEquals(values[i]); i++)
			;
		if (i < size) {
			result = (String) entities[i];
		}
		return result;
	}
	
	private static void setSummaryByValue(ListPreference prefs){
		if (prefs.getValue()!=null){
			prefs.setSummary(getEntityByValue(prefs, prefs.getValue()));
		}
		prefs.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				preference.setSummary(getEntityByValue(preference,
						newValue.toString()));
				return true;
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		strAll = getResources().getString(R.string.view_all);
		addPreferencesFromResource(R.xml.view);

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		
		setSummaryByValue((ListPreference) findPreference("viewSort"));
		setSummaryByValue((ListPreference) findPreference("viewSortDir"));
		int count;
		String pref;
		ListPreference viewGenres = (ListPreference) findPreference("viewGenre");
		pref = prefs.getString("prefGenres", getResources().getString(R.string.genres));
		String[] genres = (strAll+DIVIDER+pref).split(DIVIDER);
		count = genres.length;
		for (int i=0; i<count; i++){
			genres[i]=genres[i].trim();
		}
		viewGenres.setEntries(genres);
		viewGenres.setEntryValues(genres);
		if (viewGenres.getValue()==null) {
			viewGenres.setValue(strAll);
		}
		setSummaryByValue(viewGenres);
		
		ListPreference viewVideo = (ListPreference) findPreference("viewVideo");
		pref = prefs.getString("prefVideos", getResources().getString(R.string.videos));
		String[] videos = (strAll+DIVIDER+pref).split(DIVIDER);
		count = videos.length;
		for (int i=0; i<count; i++){
			videos[i]=videos[i].trim();
		}
		viewVideo.setEntries(videos);
		viewVideo.setEntryValues(videos);
		if (viewVideo.getValue()==null) {
			viewVideo.setValue(strAll);
		}
		setSummaryByValue(viewVideo);
		
		ListPreference viewAudio = (ListPreference) findPreference("viewAudio");
		pref = prefs.getString("prefAudios", getResources().getString(R.string.audios));
		String[] audios = (strAll+DIVIDER+pref).split(DIVIDER);
		count = audios.length;
		for (int i=0; i<count; i++){
			audios[i]=audios[i].trim();
		}
		viewAudio.setEntries(audios);
		viewAudio.setEntryValues(audios);
		if (viewAudio.getValue()==null) {
			viewAudio.setValue(strAll);
		}
		setSummaryByValue(viewAudio);
	}

}